// pages/wj/addxx/addxx.js
var app = getApp();
var edit,id;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zm: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
    zm_csh: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
    dx_show:true,
    dx2_show:true,
    dx_show_box:true,
    dx_show_box2: true,
    wd_show:true,
    edit:0,
    index: -1,
    picker: ['单选', '多选'],
    xx:[
      ['A',""]
    ]
  },
  loginForm:function(e)
  {
    console.log(e.detail.value);
    var sj = e.detail.value;
    var wt = sj.wt;
    var zdxx = sj.zdxx;
    var zxxx = sj.zxxx;
    var bdt = sj.bdt;
    var tmlx = sj.tmlx;
    if (parseInt(zxxx) > parseInt(zdxx))
    {
      wx.showModal({
        content: '多选题最少选项限制大于最大选项,请检查!',
        showCancel: false
      });
    }
    else
    {
      if(wt == ""){
        wx.showModal({
          content: '请输入必填项',
          showCancel: false
        });
      }
      else{
        if (tmlx == "1" && (zdxx == "" || zxxx == ""))
        {
          wx.showModal({
            content: '请输入多选条件',
            showCancel: false
          });
          return false;
        }
        var xx = [];
        if (tmlx == "0")//单选取值
        {
          var count = sj.danxxnr_count;
          for(var i=0;i<count;i++)
          {
            if (sj['danxxnr' + i] == ""){
              wx.showModal({
                content: '不能存在空的选项',
                showCancel: false
              });
              return false;
            }else{
              xx.push(
                { xx: sj['danxxnr'+i]}
              )
            }
          }
        } else if (tmlx == "1") {//多选取值
          if (!(/(^[1-9]\d*$)/.test(zxxx)) || !(/(^[1-9]\d*$)/.test(zdxx))) {
            wx.showModal({
              content: '最少选项和最多选项的值必须为正整数',
              showCancel: false
            });
            return false;
          }
          var count = sj.duoxxnr_count;
          for (var i = 0; i < count; i++) {
            if (sj['duoxxnr' + i] == "" ) {
              wx.showModal({
                content: '不能存在空的选项',
                showCancel: false
              });
              return false;
            } else {
              xx.push(
                { xx: sj['duoxxnr' + i] }
              )
            }
          }
        }
        var ls = {
          tm : wt,
          zdxx : zdxx,
          zxxx : zxxx,
          bdt : bdt,
          lx : tmlx,
          xx:xx
        }
        console.log(ls);
        if(edit == 1){
          app.globalData.ls_wj[0].wj_tm[id] = ls;
        }
        else{
        app.globalData.ls_wj[0].wj_tm.push(ls);
        }
        wx.navigateBack({ changed: true });//返回上一页
      }
    }
  },
  adddanxuan:function(){
    var xx = this.data.xx;
    if(xx.length >= 26 || this.data.zm.length <= 0)
    {
      wx.showModal({
        content: '选项已达上限',
        showCancel: false
      });
    }
    else
    {
      var zm = this.data.zm;
      zm.splice(0, 1);
      xx.push(
        [this.data.zm[0], ""]
      )
      this.setData({
        "xx":xx,
        "zm":zm
      })
    }
  },
  delxx:function(e){
    console.log(e.currentTarget.id);
    var xx = this.data.xx;
    xx.splice(e.currentTarget.id, 1);
    if(xx.length <= 1)
    {
      this.setData({
        "zm": ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
      })
    }
    this.setData({
      "xx":xx
    })
  },
  PickerChange(e) {
    if (e.detail.value == "0")
    {
      this.setData({
        dx_show: true,
        dx2_show: true,
        dx_show_box: true,
        dx_show_box2: true,
        wd_show: true,
        dx_show_box2: false,
        dx_show: false,
        dx_show_box:false
      })
    } else if (e.detail.value == "1") {
      this.setData({
        dx_show: true,
        dx2_show: true,
        dx_show_box: true,
        wd_show: true,
        dx_show_box2: true,
        dx2_show: false,
        dx_show_box2: false,
        dx_show_box: false
      })
    }else
    {
      this.setData({
        dx_show: true,
        dx2_show: true,
        dx_show_box: true,
        dx_show_box2: true,
        dx_show_box: false,
        wd_show: false,
      })
    }
    this.setData({
      index: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    edit = options.edit;
    if(edit != undefined)
    {
      id = options.id;
      var ls_wj = app.globalData.ls_wj[0];
      var wt_id = ls_wj.wj_tm[id].lx;
      var xx = ls_wj.wj_tm[id].xx;
      console.log(xx);
    }
    if(edit == "1")
    {
      if (wt_id == "0"){
        this.setData({
          "index":parseInt(wt_id),
          edit:edit,
          dx_show: true,
          dx2_show: true,
          dx_show_box: true,
          dx_show_box2: true,
          wd_show: true,
          dx_show_box2: false,
          dx_show: false,
          dx_show_box: false,
          "wt": ls_wj.wj_tm[id].tm,
          xx:xx,
        })
      } else if (wt_id == "1") {
        this.setData({
          "index": parseInt(wt_id),
          "wt": ls_wj.wj_tm[id].tm,
          edit: edit, 
          dx_show: true,
          dx2_show: true,
          dx_show_box: true,
          wd_show: true,
          dx_show_box2: true,
          dx2_show: false,
          dx_show_box2: false,
          dx_show_box: false,
          xx: xx,
        })
      }else{
        this.setData({
          "index": parseInt(wt_id),
          "wt": ls_wj.wj_tm[id].tm,
          edit: edit,
          dx_show: true,
          dx2_show: true,
          dx_show_box: true,
          dx_show_box2: true,
          dx_show_box: false,
          wd_show: false,
          xx: xx,
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})